//
//  HBaseLoadingViewController.m
//  SSIL
//
//  Created by 余胜民 on 16/5/13.
//  Copyright © 2016年 sanmy. All rights reserved.
//

#import "HBaseLoadingViewController.h"

@implementation HBaseLoadingViewController
- (void)viewDidLoad{
    /**
     *  初始化
     */
    [self initData];
    [super viewDidLoad];
}

- (void)setupRootView{
    
    WS(ws);
    if (_tableViewType == TableViewStylePlain) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    }
    else{
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    }
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    [_tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.view.top);
        make.left.equalTo(ws.view.left);
        make.right.equalTo(ws.view.right);
        make.bottom.equalTo(ws.view.bottom);
    }];
    
    __weak typeof (self) weakSelf = self;
    
    
    /**
     *  添加上拉刷新
     */
    
    //创建刷新的动画
    NSMutableArray *imgs = [NSMutableArray array];
    for (int i=0; i<6; i++) {
        NSString *imgName = [NSString stringWithFormat:@"refresh_img_0%d.png",i+1];
        UIImage *img = [UIImage imageNamed:imgName];
        [imgs addObject:img];
    }
    NSArray *firstImg = [imgs subarrayWithRange:NSMakeRange(0, 1)];
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setImages:firstImg forState:MJRefreshStateIdle];
    [header setImages:imgs forState:MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
    
    /**
     *  添加下拉加载
     */
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
    
    /**
     *  初始隐藏
     */
    _tableView.mj_footer.hidden = YES;
    _tableView.mj_header.hidden = YES;
    
    /**
     *  设置是否延时
     */
    if (_unDelaysContentTouches && _tableView.delaysContentTouches) {
        self.unDelaysContentTouches = YES;
    }
}

#pragma mark tableView代理

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mainDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellIdentifier = @"cellIdentifier";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont cellFont];
        cell.textLabel.textColor = [UIColor baseTextColor];
        cell.detailTextLabel.font = [UIFont cellFont];
        cell.detailTextLabel.textColor = [UIColor baseTextGrayColor];
    }
    cell.textLabel.text = _mainDataArr[indexPath.row];
    return cell;
}


#pragma mark 事件

/**
 *  初始化
 */
- (void)initData{
    [self showLoadingView];
    _mainDataArr = [NSMutableArray array];
}

/**
 *  加载更多回调函数
 */
- (void)loadMoreData{
    
}

/**
 *  下拉刷新回调函数
 */

- (void)refreshData{
    
}

- (void)endTableViewRefreshing{
    if ([_tableView.mj_header isRefreshing]) {
        [_tableView.mj_header endRefreshing];
    }
    if ([_tableView.mj_footer isRefreshing]) {
        [_tableView.mj_footer endRefreshing];
    }
}

- (void)setUnDelaysContentTouches:(BOOL)unDelaysContentTouches{
    if (unDelaysContentTouches) {
        _tableView.delaysContentTouches = NO;
        if (IsIOS8) {
            for (UIView *currentView in _tableView.subviews) {
                if ([currentView isKindOfClass:[UIScrollView class]]) {
                    ((UIScrollView *)currentView).delaysContentTouches = NO;
                    break;
                }
            }
        }
    }
    else{
        _tableView.delaysContentTouches = YES;
    }
    _unDelaysContentTouches = unDelaysContentTouches;
}

@end
